#include "Sprite.h"
#include "DirectXFramework.h"
#include <string>
using namespace std;

Sprite::Sprite()
{
	//Initialize the current textures
	D3DXMatrixIdentity(&spriteMat);
	D3DXMatrixIdentity(&spriteScale);
	D3DXMatrixIdentity(&spriteRotate);
	D3DXMatrixIdentity(&spriteTranslate);
}

void Sprite::Init()
{
	//Create a sprite object using the directX function (this only needs to be done once)
	D3DXCreateSprite(Sprite::Instance()->getDevice(),&sprite);
}

void Sprite::Draw(SpriteInfo & aSpriteInfo)
{
	D3DXMatrixIdentity(&spriteMat);
	D3DXMatrixIdentity(&spriteScale);
	D3DXMatrixIdentity(&spriteRotate);
	D3DXMatrixIdentity(&spriteTranslate);

	//Begin drawing the sprite
	sprite->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_DEPTH_FRONTTOBACK);


	D3DXMatrixTranslation(&spriteTranslate, (float)aSpriteInfo.posOfCenter.X, (float)aSpriteInfo.posOfCenter.Y, 0.0f);
	//D3DXMatrixRotationZ(&spriteRotate, D3DXToRadian(aSpriteInfo.mOrientation));
	D3DXMatrixScaling(&spriteScale, aSpriteInfo.scale, aSpriteInfo.scaleButton, aSpriteInfo.scale);

	spriteMat = spriteRotate * spriteScale * spriteTranslate;//this i feel works the best but still not perfect
	//spriteMat = spriteScale * spriteRotate * spriteTranslate;
	//spriteMat = spriteRotate * spriteTranslate * spriteScale;
	//spriteMat = spriteTranslate * spriteRotate * spriteScale;

	//Set the transform
	sprite->SetTransform(&spriteMat);

	//Draw the sprite object with the texture
	sprite->Draw(aSpriteInfo.texture,
		0, &D3DXVECTOR3( (float)aSpriteInfo.center.X, (float)aSpriteInfo.center.Y, 0.0f),
		0, D3DCOLOR_ARGB(255, 255, 255, 255));

	//End drawing the sprite
	sprite->End();
}

void Sprite::Release()
{
	sprite->Release();
}